import JSONBigint from 'json-bigint';
import { FieldType, Size } from '@/interface';

export default ({
  projectId,
  id,
  dyDetailStore,
  recordId,
}:{
projectId:string,
id:string,
dyDetailStore:any,
recordId?:string,
}) :object => ({

  autoQuery: false,
  autoCreate: false,
  selection: false,
  pageSize: 22,
  fields: [
    {
      name: 'id',
      type: 'string' as FieldType,
    },
    {
      name: 'name',
      type: 'string' as FieldType,
    },
    {
      name: 'status',
      type: 'string' as FieldType,
    },
    {
      name: 'stageRecordList',
      type: 'object' as FieldType,

    },
    {
      name: 'trigger',
      type: 'object' as FieldType,

    },
    {
      name: 'triggerType',
      type: 'string' as FieldType,
    },
    {
      name: 'finishedDate',
      type: 'string' as FieldType,
    },
    {
      name: 'staredDate',
      type: 'string' as FieldType,
    },
    {
      name: 'enable',
      type: 'boolean' as FieldType,
    },
  ],
  transport: {
    read: (data:any) => {
      const flag = data?.data?.audit_flag;
      if (flag === true) {
        return {
          url: `devops/v1/projects/${projectId}/pipeline_records/paging?pipeline_id=${id}&audit_flag=${flag}`,
          method: 'get',
          transformResponse(response:any) {
            try {
              const treeData = JSONBigint.parse(response);
              dyDetailStore.setTreeDataHasMore(treeData.totalElements > 0
                && (treeData.number + 1) < treeData.totalPages);
              return treeData;
            } catch (error) {
              return response;
            }
          },
        };
      }
      return {
        url: `devops/v1/projects/${projectId}/pipeline_records/paging?pipeline_id=${id}&audit_flag=${false}`,
        method: 'get',
        transformResponse(response:any) {
          try {
            const treeData = JSONBigint.parse(response);
            dyDetailStore.setTreeDataHasMore(treeData.totalElements > 0
              && (treeData.number + 1) < treeData.totalPages);
            return treeData;
          } catch (error) {
            return response;
          }
        },
      };
    },

  },
  events: {
    // 监听ds数据加载完成的时间事件，方便在网络请求成功就拿到数据，不用等load完成，可能会有时间差
    beforeLoad: ({ dataSet, data }:any) => {
      // data.find((item:any, index:number) => {
      //   if (item.id === recordId) {
      //     console.log('idd', index);
      //     dyDetailStore.setAuditFlag(true);
      //     dyDetailStore.setIndex(index);
      //     dyDetailStore.setFlag(1);
      //   }
      // });
    },
  },
});
